package api.http.file.api;

import android.util.Log;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.io.File;

import api.base.bean.BasicResponse;
import api.base.impl.BaseImpl;
import api.base.observer.DefaultObserver;
import api.http.file.bean.UpLoatBean;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018-01-12.
 */

public class UpdataFileImpl extends BaseImpl<UpdataFileService> implements IUpdataFile<String> {
    @Override
    public void upload(RxAppCompatActivity context, String filename, DefaultObserver<BasicResponse<UpLoatBean>> observer) {
       String path= context.getCacheDir()+ File.separator+"zhw/"+"zhw.png";
        Log.e("UpdataFileImpl","path    "+path);

        File file = new File(path);
// /data/data/utils.utilcode.blankj.com.nettext/cache/zhw/zhw.txt
        Log.e("UpdataFileImpl",file.exists()+"");

        // 创建 RequestBody，用于封装构建RequestBody
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//         MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

        // 添加描述
        String descriptionString = "hello, 这是文件描述";
        RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
        scheduler(mService.upload(description,body))
                .compose(context.<BasicResponse<UpLoatBean>>bindToLifecycle())
                .subscribe(observer);
    }
}
